# Copyright (c) 2013, 2015 Ruslan Baratov, Alexandre Pretyman
# All rights reserved.

cmake_minimum_required(VERSION 3.5)
project(Hunter)

list(APPEND CMAKE_MODULE_PATH "@HUNTER_SELF@/cmake/modules")

include(hunter_libsodium_install_tools)
include(hunter_user_error)
include(hunter_status_debug)
include(hunter_assert_not_empty_string)
include(hunter_autotools_project)

hunter_status_debug("Scheme: url_sha1_libsodium_autogen_autotools")

if("@MSVC@")
    hunter_user_error("Autotools scheme not supported with Visual Studio")
endif()

# Check preconditions
hunter_assert_not_empty_string("@HUNTER_SELF@")
hunter_assert_not_empty_string("@HUNTER_EP_NAME@")
hunter_assert_not_empty_string("@HUNTER_PACKAGE_URL@")
hunter_assert_not_empty_string("@HUNTER_PACKAGE_SHA1@")
hunter_assert_not_empty_string("@HUNTER_PACKAGE_DOWNLOAD_DIR@")
hunter_assert_not_empty_string("@HUNTER_PACKAGE_SOURCE_DIR@")
hunter_assert_not_empty_string("@HUNTER_PACKAGE_BUILD_DIR@")
hunter_assert_not_empty_string("@HUNTER_PACKAGE_CONFIGURATION_TYPES@")
hunter_assert_not_empty_string("@HUNTER_PACKAGE_INSTALL_PREFIX@")
hunter_assert_not_empty_string("@HUNTER_INSTALL_PREFIX@")

set(previous_project "")

# Autotools Project
foreach(build_config_type @HUNTER_PACKAGE_CONFIGURATION_TYPES@)

    # Check for valid configuration
    string(COMPARE EQUAL "${build_config_type}" "Release" is_release)
    string(COMPARE EQUAL "${build_config_type}" "Debug"   is_debug)
    if(NOT (is_release OR is_debug))
        hunter_user_error("libsodium: Invalid build configuration type provided. Valid values are: \"Release\", \"Debug\", \"Release\;Debug\" (HUNTER_PACKAGE_CONFIGURATION_TYPES)")
    endif()

    set(current_project "@HUNTER_EP_NAME@${build_config_type}")

    hunter_autotools_project("${current_project}"
        HUNTER_SELF
        "@HUNTER_SELF@"
        URL
        @HUNTER_PACKAGE_URL@
        URL_HASH
        SHA1=@HUNTER_PACKAGE_SHA1@
        DOWNLOAD_DIR
        "@HUNTER_PACKAGE_DOWNLOAD_DIR@"
        SOURCE_DIR
        "@HUNTER_PACKAGE_SOURCE_DIR@"
        BUILD_DIR
        "@HUNTER_PACKAGE_BUILD_DIR@"
        INSTALL_DIR
        "@HUNTER_PACKAGE_INSTALL_PREFIX@"
        GLOBAL_INSTALL_DIR
        "@HUNTER_INSTALL_PREFIX@"
        PARALLEL_JOBS
        "@HUNTER_JOBS_OPTION@"
        PACKAGE_CONFIGURATION_TYPES
        "@HUNTER_PACKAGE_CONFIGURATION_TYPES@"
        BOOTSTRAP
        "./autogen.sh"
        EXTRA_FLAGS
        --disable-soname-versions
    )

    # Rename the libraries, needed in the case we have
    # debug and release builds at the same. We don't want
    # to overwrite the library files when building the
    # second build config.
    if(BUILD_SHARED_LIBS)
        set(lib_file      "@HUNTER_PACKAGE_INSTALL_PREFIX@/lib/libsodium.so")
        set(lib_extension "so")
    else()
        set(lib_file      "@HUNTER_PACKAGE_INSTALL_PREFIX@/lib/libsodium.a")
        set(lib_extension "a")
    endif()

    if(is_debug)
        set(lib_suffix "d")
    endif()

    if(is_release)
        set(lib_suffix "r")
    endif()

    ExternalProject_Add_Step(@HUNTER_EP_NAME@${build_config_type} rename
        COMMAND ${CMAKE_COMMAND} -E rename ${lib_file} @HUNTER_PACKAGE_INSTALL_PREFIX@/lib/libsodium${lib_suffix}.${lib_extension}
        DEPENDEES install
    )


    # Install target file
    # Required: lib file name, BUILD_SHARED_LIBS, install prefix, hunter_self
    string(TOUPPER ${build_config_type} TARGET_CONFIGURATION)
    set(HUNTER_PACKAGE_INSTALL_PREFIX "@HUNTER_PACKAGE_INSTALL_PREFIX@")
    set(TARGET_LIB_FILE               "libsodium${lib_suffix}.${lib_extension}")
    set(HUNTER_SELF                   "@HUNTER_SELF@")

    # Each external project must depends on previous one since they all use
    # the same building directory
    string(COMPARE EQUAL "${previous_project}" "" is_empty)
    if(NOT is_empty)
        add_dependencies(
                "${current_project}"
                "${previous_project}"
        )
    endif()
    set(previous_project "${current_project}")

    hunter_install_libsodium_targets()

endforeach()


# Forward some variables
set(BUILD_SHARED_LIBS                  "@BUILD_SHARED_LIBS@")
set(HUNTER_libsodium_VERSION           "@HUNTER_libsodium_VERSION@")
set(HUNTER_PACKAGE_CONFIGURATION_TYPES "@HUNTER_PACKAGE_CONFIGURATION_TYPES@")

hunter_install_libsodium_config()
